package GongNeng;

import GJ.ArrayList;
import GJ.Time;
import Main.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class Return extends JFrame {

    JLabel label;
    JLabel label2;
    JLabel label3;
    JPanel panel;
    JButton button;
    JButton button2;
    JTextField textField;
    JTextField textField2;



    public static void main(String args[]){
        Return turn = new Return();
        turn.setVisible(true);
    }

    public Return(){
        super();
        setTitle("图书归还");
        setBounds(350,100,1200,800);
        getContentPane().setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setBounds(0,0,1200,800);
        panel.setLayout(null);
        getContentPane().add(panel);

        label = new JLabel("图书归还");
        label.setBounds(540,100,120,50);
        label.setFont(new Font("",Font.BOLD,25));
        panel.add(label);

        label2 = new JLabel("图书名：");
        label2.setBounds(380,200,120,50);
        label2.setFont(new Font("",Font.BOLD,23));
        panel.add(label2);

        label3 = new JLabel("借阅人：");
        label3.setBounds(380,260,120,50);
        label3.setFont(new Font("",Font.BOLD,23));
        panel.add(label3);

        textField = new JTextField();
        textField.setBounds(480,205,250,40);
        panel.add(textField);


        textField2 = new JTextField();
        textField2.setBounds(480,265,250,40);
        panel.add(textField2);


        button = new JButton("确认");
        button.setBounds(480,320,80,50);
        panel.add(button);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File file2 = new File("D://BorrowedBooks");
                String nbook = "借阅图书："+textField.getText();
                String nname = "借阅人："+textField2.getText();
                try {
                    BufferedReader bufferedReader = new BufferedReader(new FileReader(file2));
                    ArrayList arrayList = new ArrayList(100);
                    String line;
                    String[] song;
                    int sum;
                    int zhen;
                    int zhen2;
                        while ((line = bufferedReader.readLine())!= null) {
                        song = line.split(";");
                        for (int i=0;i<song.length;i++){
                            arrayList.addBooks(song[i]);
                        }
                        String name = arrayList.get(0);
                        String bookName = arrayList.get(1);
                        if (name!=null){
                            if (name.equals(nname)&&bookName.equals(nbook)){
                                String s1 =arrayList.get(7);//秒
                                String s2 =arrayList.get(6);//分钟
                                zhen2 = Integer.parseInt(s2);//获取借阅时的分钟
                                zhen = Integer.parseInt(s1);//获取借阅时的秒
                                Time time = new Time();int string1 = time.returnSecond();//获取还书秒
                                int string2 = time.returnMinute();//获取还书分钟
                                if (string2==zhen2){//如果分钟数相同，则秒数相减
                                    sum = (string1-zhen)*2;JOptionPane.showMessageDialog(null,"借书已超时"+(string1-zhen)+"秒"+";"+"需要支付违约金："+sum+"元");
                                }else if (string2<zhen2){
                                    sum = (zhen2-string2)*60*2+(60-zhen+string1)*2;int minute1 = (zhen2-string2);
                                    int miao = (60-zhen2+string2);JOptionPane.showMessageDialog(null,"借书已超时"+minute1+"分"+miao+"秒"+";"+"需要额外支付违约金："+sum+"元");
                                }else{ sum = (string2-zhen2)*60*2+(60-zhen+string1)*2;
                                    int minute2 = (string2-zhen2);int miao1 = (60-zhen+string1);
                                    JOptionPane.showMessageDialog(null,"借书已超时"+minute2+"分"+miao1+"秒"+";"+"需要支付违约金"+sum+"元");
                                }
                            }else{
                                JOptionPane.showMessageDialog(null,"你并没有在此借书！");
                            }
                        }else{
                            JOptionPane.showMessageDialog(null,"书库为空！");
                        }

                      /*  String s1 =arrayList.get(7);//秒
                        String s2 =arrayList.get(6);//分钟
                        zhen2 = Integer.parseInt(s2);//获取借阅时的分钟
                        zhen = Integer.parseInt(s1);//获取借阅时的秒
                        Time time = new Time();int string1 = time.returnSecond();//获取还书秒
                        int string2 = time.returnMinute();//获取还书分钟
                        if (string2==zhen2){//如果分钟数相同，则秒数相减
                            sum = (string1-zhen)*2;JOptionPane.showMessageDialog(null,"借书已超时"+(string1-zhen)+"秒"+";"+"需要支付违约金："+sum+"元");
                        }else if (string2<zhen2){
                            sum = (zhen2-string2)*60*2+(60-zhen+string1)*2;int minute1 = (zhen2-string2);
                            int miao = (60-zhen2+string2);JOptionPane.showMessageDialog(null,"借书已超时"+minute1+"分"+miao+"秒"+";"+"需要额外支付违约金："+sum+"元");
                        }else{ sum = (string2-zhen2)*60*2+(60-zhen+string1)*2;
                            int minute2 = (string2-zhen2);int miao1 = (60-zhen+string1);
                            JOptionPane.showMessageDialog(null,"借书已超时"+minute2+"分"+miao1+"秒"+";"+"需要支付违约金"+sum+"元");
                        }*/
                    }
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });

        button2 = new JButton("退出");
        button2.setBounds(650,320,80,50);
        panel.add(button2);
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String buttonName = e.getActionCommand();
                if (buttonName.equals("退出")){
                    dispose();
                    new Main().setVisible(true);
                }
            }
        });
    }

    public void returnMonen() throws IOException {
        Borrow borrow = new Borrow();
       // borrow.returnMoney();
       // System.out.println(borrow.returnMoney());
    }


}
